package game;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/9/2 22:52:39
 */
public class Enemy extends Thread{
    private int x;
    private int y;
    private int w;
    private int h;
    private int speed;
    private int bloodW;
    private int bloodH;
    private BufferedImage image;
    private List<BufferedImage> listImage = new ArrayList<>();
    private GamePanel gamePanel;
    private Hero hero;
    private HeroBlood heroBlood;

    Random rd = new Random();

    public Enemy(GamePanel gamePanel, Hero hero, HeroBlood heroBlood){
        this.heroBlood = heroBlood;
        this.gamePanel = gamePanel;
        this.hero = hero;
        this.speed = rd.nextInt(10);
        this.bloodW = 100;
        this.bloodH = 15;
        this.x = rd.nextInt(4000) + GameFrame.W;
        this.y = rd.nextInt(280) + 270;
        for (int i = 0; i < 16; i++) {
            this.listImage.add(ImageUtil.getImg("../img/enemy/" + (i + 1) + ".png"));
        }
        this.image = this.listImage.get(0);
        this.w = this.image.getWidth();
        this.h = this.image.getHeight();
    }

    /**
    * @Author: liwan
    * @Description: 怪物移动
    * @Params:
    * @Return:
    * @DateTime: 2021/9/2 22:58:30
    */
    int index = 1;
    public void move(){
        this.x -= this.speed;
        index++;
        index = index < 16 ? index : 0;
        this.image = this.listImage.get(index);
        gamePanel.repaint();
    }

    public void att(){
        if(this.x + 50 > hero.getX() && this.x - 50 < hero.getX() && this.y + 50 > hero.getY() && this.y - 50 < hero.getY()){
            if(this.heroBlood.getW() - 3 >= 0){
                this.heroBlood.setW(this.heroBlood.getW() - 3);
                this.gamePanel.repaint();
            }
        }
    }

    @Override
    public void run() {
        while (true) {
            this.move();
            this.att();
            if (this.bloodW <= 0) {
                this.x = rd.nextInt(4000) + GameFrame.W;
                this.y = rd.nextInt(280) + 270;
                this.w = 100;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public int getBloodW() {
        return bloodW;
    }

    public void setBloodW(int bloodW) {
        this.bloodW = bloodW;
    }

    public int getBloodH() {
        return bloodH;
    }

    public void setBloodH(int bloodH) {
        this.bloodH = bloodH;
    }
}
